package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Reference
	private UserService userService;

	//注入角色业务层
	@Reference
	private RoleService roleService;

	@RequestMapping("/getUsername")
	public Result getUsername(){
		log.debug("获取用户名!!");
		//获取安全框架上下文对象
		SecurityContext context = SecurityContextHolder.getContext();
		//获取认证对象
		Authentication authentication = context.getAuthentication();
		//获取 Principal 重要对象
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){
			User user = (User) principal;
			String username = user.getUsername();
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
		}
		return new Result(false,MessageConst.GET_USERNAME_FAIL);
	}

	@RequestMapping("/loginSuccess")
	public ModelAndView loginSuccess(){
		return new ModelAndView("redirect:http://oms.web.health:83/pages/main.html");
	}

	@RequestMapping("/loginError")
	public ModelAndView doError(){
		return new ModelAndView("redirect:http://oms.web.health:83/login.html");
	}

	//根据用户名查询菜单
	@RequestMapping("findMenuByUserName")
	public Result findMenuByUserName(String uname){
		log.debug("UserController---findMenuByUserName: "+uname);
		List<Map<String,Object>> menuList = userService.findMenuByUserName(uname);
		log.debug("查询菜单成功!!"+menuList);
		return new Result(true,"查询菜单成功!!",menuList);
	}



//	@RequestMapping("/login")
//	public Result login(String username, String password){
//		log.debug("oms backend,user:"+username+" ,password:"+password);
//		if(userService.login(username,password)){
//			log.debug("login ok!!!");
//			return new Result(true, MessageConst.ACTION_SUCCESS);
//		}else{
//			log.debug("login fail");
//			return new Result(false,MessageConst.ACTION_FAIL);
//		}
//	}

	/**
	 * 用户的增删改查
	 * */

	//1用户新增 addUser  点击确定将用户基本信息和角色信息分别提交到创建用户 两个参数一个用户基本信息  另一个用户信息添加
		@RequestMapping("addUser")
	public Result addUser(@RequestBody SysUser sysUser, Integer[] checkitemIds) {
		log.debug("Usercontroller---addUser" + sysUser + checkitemIds);
		userService.addUser(sysUser, checkitemIds);
		log.debug("添加新用户成功!!!");
		return new Result(true, "添加新用户成功!");
	}

	//5新增用户时 查询角色信息回显 findAllRole axios异步提交数据回显所有角色信息
	@RequestMapping("findAllRole")
	public Result findAllRole(){
		log.debug("UserController---findAllRole");
		List<Role> roleList = roleService.findAll();
		log.debug("查询所有角色信息成功"+roleList);
		//返回数据
		return new Result(true,"查询所有角色信息成功",roleList);
	}


	//2用户删除 deleteUserById  根据用户i删除用户
	@RequestMapping("deleteUserById")
	public Result deleteUserById(Integer id) {
		log.debug("UserController---deleteUserById: " + id);
		userService.deleteUserById(id);
		log.debug("根据id删除用户成功!!!");
		return new Result(true,"删除用户成功");
	}


	//3用户修改 updateUser  点击提交直接修改 用户信息 需要修改关联的角色表
	@RequestMapping("updateUser")
	public Result updateUser(@RequestBody SysUser sysUser,Integer[] checkitemIds){
		log.debug("UserController---updateUser"+sysUser);
		log.debug("UserController---updateUser"+ Arrays.toString(checkitemIds));
		userService.updateUser(sysUser,checkitemIds);
		log.debug("修改用户成功"+sysUser);
		return new Result(true,"修改用户信息成功!");
	}
	//6修改时根据用户id 基本数据的回显  findByUserId
	@RequestMapping("findByUserId")
	public Result findByUserId(Integer id){
		log.debug("UserController---findByUserId: "+id);
		SysUser sysUser = userService.findByUserId(id);
		log.debug("根据id查询成功"+sysUser.toString());
		return new Result(true,"查询会员成功",sysUser);
	}
	//7修改用户时根据用户id 已经拥有的角色信息的回显 findRoleIdByUserId
	@RequestMapping("findRoleIdByUserId")
	public Result findRoleIdByUserId(Integer userId){
		log.debug("UserController---findRoleIdByUserId: "+userId);
		List<Integer> roleIdlist = userService.findRoleIdByUserId(userId);
		log.debug("查询角色id成功"+roleIdlist);
		return new Result(true,"查询角色id成功",roleIdlist);
	}


	//4用户查询 分页查询  初始化界面 findUserByPage
	@RequestMapping("findPage")
	public PageResult findPage(@RequestBody QueryPageBean pageBean){
		log.debug("UserController---findPage");
		PageResult pageResult = userService.findPage(pageBean);
		log.debug("findPage: "+pageResult);
		return pageResult;
	}
}